package com.robert.jfinal.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.robert.jfinal.model.Member;
import com.robert.jfinal.validator.MemberValidator;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.List;

/**
 * Created by zhenghao on 2016/3/22.
 *
 */
public class MemberController extends Controller {


    private static final Logger logger=Logger.getLogger(MemberController.class);

    public void list(){

        logger.info("--------------- in  member controller list");

        List<Record> memberList= Db.find("select * from member order by id");

        setAttr("memberList",memberList);

        render("list.html");
    }


    public void jsonList(){

        List<Record> memberList=Db.find("select * from member order by id");

        setAttr("memberList",memberList);

        renderJson();
    }



    public void add(){

        render("add.html");
    }


    @Before(MemberValidator.class)  //验证表单值
    public void submitAdd(){

        //从表单构建对象
        Member member=this.getModel(Member.class);

        //对象设置属性
        member.set("createTime",new Date());

        member.save();

        forwardAction("/member/list");
    }


    public void delete(){

        Db.deleteById("member", this.getParaToInt(0));

        forwardAction("/member/list");
    }




}
